/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of linkedup
**
** linkedup is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** linkedup is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0
import com.nokia.extras 1.0
import "home"
import "components"

PageStackWindow {
    id: appWindow
	initialPage: Page{tools: commonTools}

	signal postFinished(string responseText, string statusCode, string statusText)

	function createHome(){
		var home = homeScreen.createObject(appWindow)
		setCompaniesFollowed(API.get_person_following_company())
		setJobsBookmarked(API.get_person_bookmark_job())
		setPeopleConnections(API.get_connections_by_id("~"))
		pageStack.push(home)
	}

	function getAuthorization(){
		var page = loginScreen.createObject(appWindow)
		pageStack.push(page)
	}

	Timer{id: homeTimer
		interval: 1000
		onTriggered: createHome()
	}

	/**
	  Downloads the companies that the user is currently following
	  and hashes them in the User class. Linkedin does not report
	  whether the user is currently following a company when looking
	  at that company, so this is how I can determine before hand.
	  */
	function setCompaniesFollowed(url){
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				User.setCompaniesFollowed(xmlHttp.responseText)
			}
		}
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	}

	/**
	  Same concept as the setCompaniesFollowed function, but
	  instead for dealing with the current member connections
	  */
	function setPeopleConnections(url){
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				User.setPeopleConnections(xmlHttp.responseText)
			}
		}
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	}

	/**
	  Same concept as the setCompaniesFollowed function, but
	  instead for dealing with the current member job bookmarks
	  */
	function setJobsBookmarked(url){
		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				User.setJobsBookmarked(xmlHttp.responseText)
			}
		}
		xmlHttp.open( "GET", url, true );
		xmlHttp.send( null );
	}



	Component.onCompleted: {
		theme.inverted = true
		if(Settings.get("oauth_token") === "" || Settings.get("oauth_token_secret") === "")
			getAuthorization()
		else{
			createHome()
		}
	}


    ToolBarLayout {
        id: commonTools
        visible: true
		ToolIcon { platformIconId: "toolbar-view-menu";
			anchors.right: parent===undefined ? undefined : parent.right
			onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
		}
    }

	Menu {
		id: myMenu
		visualParent: pageStack
		MenuLayout {
			MenuItem { text: "post status"; onClicked: statusSheet.status == DialogStatus.Open ? statusSheet.close() : statusSheet.open()}
			MenuItem { text: "invert theme"; onClicked: theme.inverted = !theme.inverted}
		}
	}


	onPostFinished: {
		var obj = banner.createObject(pageStack, {"text": (statusCode == "201" ? "Share sent" : "Share could not be sent") })
		obj.show()
		obj.destroy(5000)
	}

	Component{
		id: banner
		InfoBanner{timerEnabled: true; timerShowTime: 3000; z:100}
	}

	Component{
		id: loginScreen
		LoginScreen{onAuthorized: homeTimer.start()}
	}

	Component{
		id: homeScreen
		Home{}
	}

	LinkedInStatusSheet{
		id: statusSheet
		z: 1000
		visualParent: pageStack
		onSend: post(API.post_share(xml))
	}

	function post(list){
		var xmlHttp = new XMLHttpRequest();
		var url = list[0]
		var header = list[1]
		var body = list[2]

		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				appWindow.postFinished(xmlHttp.responseText, xmlHttp.status, xmlHttp.statusText)
			}
		}
		xmlHttp.open( "POST", url, true );
		xmlHttp.setRequestHeader("Content-Type", "text/xml")
		xmlHttp.setRequestHeader("Host", "api.linkedin.com");
		xmlHttp.setRequestHeader("Authorization", header);
		xmlHttp.send( body );
	}



}
